Shader "Unlit/lightprobe"
{
    Properties
    {
        _MainTex ("_MainTex", 2D) = "white" {}
        _MeanTex ("_MeanTex", 2D) = "white" {}
        _AxesTex ("_AxesTex", 2D) = "white" {}
        _CoeffTex ("_CoeffTex", 2D) = "white" {}
        
        _LightProbeIdx ("_LightProbeIdx", Float) = 0
        _ByCompress ("_ByCompress", Float) = 0
        _ComponentCount ("_ComponentCount", Float) = 0

        _Result ("_Result", Color) = (0,0,0,0)
        
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "LightMode"="UniversalForward" }
        LOD 100

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            #include "./CompressProbe.hlsl"

            struct Attributes
            {
                float4 positionOS : POSITION;
                float3 normalOS : NORMAL;
                float2 uv : TEXCOORD0;
            };

            struct Varings
            {
                float4 positionCS : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 positionWS : TEXCOORD1;
                float3 normalWS : TEXCOORD2;
                float3 viewDirWS : TEXCOORD3;
            };

            //CBUFFER_START(UnityPerMaterial)
            float4 _BaseColor;
            float4 _MainTex_ST;
            float _LightProbeIdx;
            float _ByCompress;
            float4 _Result;
            //CBUFFER_END
            
            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);
            
            Varings vert (Attributes IN)
            {
                Varings OUT;
                // GPU Instancing
                // UNITY_SETUP_INSTANCE_ID(IN);
                // UNITY_TRANSFER_INSTANCE_ID(IN, OUT);
                
                VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz);
                VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS.xyz);
                //OUT.positionCS = TransformObjectToHClip(IN.positionOS);
                OUT.positionCS = positionInputs.positionCS;
                OUT.positionWS = positionInputs.positionWS;
                OUT.viewDirWS = GetCameraPositionWS() - positionInputs.positionWS;
                OUT.normalWS = normalInputs.normalWS;
                OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
                return OUT;
            }

            float4 frag (Varings IN) : SV_Target
            {
                ////GPU Instancing
                //UNITY_SETUP_INSTANCE_ID(IN);
                //half4 mainColor = UNITY_ACCESS_INSTANCED_PROP(PerInstance, _MainColor);

                // light
                float4 shadowCoord = TransformWorldToShadowCoord(IN.positionWS.xyz);
                Light light = GetMainLight();
                float3 lightDirWS = light.direction;
                float3 lightColor = light.color;
                float lightIntensity = light.distanceAttenuation;

                //return float4(lightDirWS,1);
                
                _Result = _BaseColor;

                //_LightProbeIdx = 0;
                //lightDirWS  = float3(0,1,0);
                
                if (_ByCompress)
                {
                    _Result = float4(DecodeSH(_LightProbeIdx, 12, lightDirWS), 0);
                    // if (_Result.r < 0)
                    // {
                    //     return float4(1,0,0,1);
                    // }
                    return _Result;
                }

                return _Result;

                //采样纹理
                // half4 texColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);  
                // Diffuse lighting
                //half3 diffuse = LightingLambert(light.color, light.direction, IN.normalWS);
                // float3 normal = normalize(IN.normalWS);
                // float NdotL = saturate(dot(normal, lightDirWS));
                // half3 diffuse = _MainColor.xyz * texColor.xyz *lightColor * NdotL;

                // Specular lighting (Blinn-Phong)
                //half3 specular = LightingSpecular(light.color, light.direction, normalize(IN.normalWS), normalize(IN.viewDirWS), _SpecularColor, _Smoothness);
                // float metallic = _Metallic;
                // float smoothness = _Smoothness;
                // float3 viewDir = IN.viewDirWS;
                // float3 halfDir = normalize(lightDirWS + viewDir);
                // float NdotH = saturate(dot(normal, halfDir));
                // float specularIntensity = pow(NdotH, smoothness * 100.0);
                // half3 specular = lightColor * lightIntensity * specularIntensity * metallic;

                //逐顶点光源
                //half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
                //逐像素光源
                // uint pixelLightCount = GetAdditionalLightsCount();
                // for (uint lightIndex = 0; lightIndex < pixelLightCount; ++lightIndex)
                // {
                //     Light light = GetAdditionalLight(lightIndex, IN.positionWS);
                //     diffuse += LightingLambert(light.color, light.direction, IN.normalWS);
                //     specular += LightingSpecular(light.color, light.direction, normalize(IN.normalWS), normalize(IN.viewDirWS), _SpecularColor, _Smoothness);
                // }
                
                // Shadow
                // float shadow = light.shadowAttenuation;

                // half4 finalColor = half4(diffuse + specular,texColor.a) * shadow;

                //return finalColor;
            }
            ENDHLSL
        }
    }
}
